import React, { Component } from 'react'
import { withRouter } from 'react-router'
import routePathConfig from '../routePathConfig'
import { Route, Redirect } from 'react-router-dom'
import { getStorage } from '../utils/storageUtils'
class navigationGuard extends Component {
    render() {
        const url = this.props.history.location.pathname
        const firstRoute = '/' + url.split('/')[1]

        let matchRoute
        if (firstRoute != '/') {
            matchRoute = routePathConfig.find(r => r.key == firstRoute)
            // console.log('matchRoute', matchRoute);
            if (matchRoute.auth) {
                if (getStorage('token')) {
                    return (
                        <Route path={matchRoute.key} component={matchRoute.component}></Route>
                    )
                } else {
                    return <Redirect from={matchRoute.key} to="/login"></Redirect>
                }
            } else {
                return (
                    <Route path={matchRoute.key} component={matchRoute.component}></Route>
                )
            }
        } else {
            return <Redirect from="" to="/login"></Redirect>
        }



    }
}
export default withRouter(navigationGuard)